/**
 * 
 */
package com.and.xmppsdk.adapter;

import java.net.URI;
import java.net.URISyntaxException;

import org.drl.xmpp.Contact;
import org.drl.xmpp.TMessage;
import org.drl.xmpp.UserInfo;
import org.drl.xmpp.storage.table.MessageTable;
import org.drl.xmpp.util.StringUtils2;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.view.ViewPager.LayoutParams;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.and.xmppsdk.R;
import com.and.xmppsdk.util.ChatFaceExpressionUtil;
import com.and.xmppsdk.util.DateTimeFormatter;

/**
 * 聊天信息数据适配器
 * @author hljdrl@gmail.com
 *
 */
public class MessageAdapter extends CursorAdapter {
	
	private LayoutInflater mInflater;
	private Context mContext;
	private final UserInfo mUserInfo;
	private final Contact mContact;
	public MessageAdapter(Context context, Cursor c,UserInfo user,Contact contact) {
		super(context, c);
		this.mContext = context;
		mInflater = LayoutInflater.from(this.mContext);
		mUserInfo = user;
		mContact = contact;
	}

	@Override
	public void changeCursor(Cursor cursor) {
		super.changeCursor(cursor);
	}
	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		setView(null, view,cursor, null);
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		View view = mInflater.inflate(R.layout.chat_conversation_msg, null);
		setView(null, view,cursor, null);
		return view;
	}
	
	private void setView(OnClickListener clickListener, View view,Cursor c,
			OnLongClickListener longClickListener) {
		TMessage msg = MessageTable.readLineTMessage(c);
		/**
		 *  消息布局
		 */
		RelativeLayout mRelativeLayout = (RelativeLayout) view.findViewById(R.id.chat_conversation_msg_layout);
		
		/**
		 * 时间
		 */
		TextView mTimeView = (TextView) view.findViewById(R.id.chat_conversation_msg_time);
		
		/**
		 * 头像
		 */
		ImageView mPhoto = (ImageView) view.findViewById(R.id.chat_conversation_msg_photo);
		ImageView mPhotoBg = (ImageView) view.findViewById(R.id.chat_conversation_msg_photo_1);
		
//		mPhoto.setOnClickListener(mPhotoOnClickListener);
		
		/**
		 * 文字、表情消息
		 */
		TextView mInput = (TextView) view.findViewById(R.id.chat_conversation_msg_text);
		/**
		 * 语音消息
		 */
		final ImageButton mInput_media = (ImageButton) view.findViewById(R.id.chat_conversation_msg_media);
		mInput_media.setOnClickListener(clickListener);
		mInput_media.setOnLongClickListener(longClickListener);
		mInput_media.setTag(c.getPosition());
		/**
		 * 语音时长
		 */
		TextView mInput_media_length = (TextView) view.findViewById(R.id.chat_conversation_msg_media_length);
		//
		RelativeLayout.LayoutParams layout = ((RelativeLayout.LayoutParams) mRelativeLayout.getLayoutParams());
//		RelativeLayout.LayoutParams time = ((RelativeLayout.LayoutParams) mTimeView.getLayoutParams());
		RelativeLayout.LayoutParams photo = ((RelativeLayout.LayoutParams) mPhoto.getLayoutParams());
		RelativeLayout.LayoutParams photo_01 = ((RelativeLayout.LayoutParams) mPhotoBg.getLayoutParams());
		RelativeLayout.LayoutParams msg_text = ((RelativeLayout.LayoutParams) mInput.getLayoutParams());
		RelativeLayout.LayoutParams msg_media = ((RelativeLayout.LayoutParams) mInput_media.getLayoutParams());
		RelativeLayout.LayoutParams msg_media_lendth = ((RelativeLayout.LayoutParams) mInput_media_length.getLayoutParams());
		String key = StringUtils2.parseName(mUserInfo.getJid());
		String toKey = StringUtils2.parseName(msg.getTo());
		if (key.equals(toKey)) { // 发给对方的消息
			
			mPhoto.setTag(mUserInfo);
			layout.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0); // 右侧布局
			layout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, -1);
//			time.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0); // 时间居中
//			time.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, -1);
			photo.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0); // 头像居右
			photo.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, -1);
			photo_01.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0); // 头像居右
			photo_01.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, -1);
			
			//
			msg_text.addRule(RelativeLayout.RIGHT_OF, 0); // 文字居右
			msg_text.addRule(RelativeLayout.LEFT_OF, R.id.chat_conversation_msg_photo); // 在头像左边
			mInput.setBackgroundResource(R.drawable.chat_conversation_balloon_right); // 文字表情背景
			
			msg_media.addRule(RelativeLayout.RIGHT_OF, 0); // 录音居右
			msg_media.addRule(RelativeLayout.LEFT_OF, R.id.chat_conversation_msg_photo); // 在头像左边
			mInput_media.setBackgroundResource(R.drawable.chat_conversation_balloon_right);
			mInput_media.setImageResource(R.drawable.chat_voice_to_palying);
			
			msg_media_lendth.addRule(RelativeLayout.RIGHT_OF, 0); // 录音时长居右
			msg_media_lendth.addRule(RelativeLayout.LEFT_OF, R.id.chat_conversation_msg_media); // 在录音左边
		
//			if (mUserInfo.isAvatar()) {
//				if (mAvatorManager.containsKeyAsAvatorBytes(mUserInfo.getJid())) {
//					mPhoto.setImageBitmap(mAvatorManager.get(mUserInfo.getJid()));
//				} else {
//					mAvatorManager.put(mUserInfo.getJid(), mUserInfo.getAvatorFile());
//					mPhoto.setImageBitmap(mAvatorManager.get(mUserInfo.getJid()));
//				}
//			}else {
//				mPhoto.setImageBitmap(null);
//			}
		} else { // 对方发来的消息
			mPhoto.setTag(mContact);
			layout.addRule(RelativeLayout.ALIGN_PARENT_LEFT, -1);
			layout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
			
			
			photo.addRule(RelativeLayout.ALIGN_PARENT_LEFT, -1);
			photo.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
			photo_01.addRule(RelativeLayout.ALIGN_PARENT_LEFT, -1);
			photo_01.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
			
			
			msg_text.addRule(RelativeLayout.RIGHT_OF, R.id.chat_conversation_msg_photo);
			msg_text.addRule(RelativeLayout.LEFT_OF, 0);
			mInput.setBackgroundResource(R.drawable.chat_conversation_balloon_left);
			
			msg_media.addRule(RelativeLayout.RIGHT_OF, R.id.chat_conversation_msg_photo);
			msg_media.addRule(RelativeLayout.LEFT_OF, 0);
			mInput_media.setBackgroundResource(R.drawable.chat_conversation_balloon_left);
			mInput_media.setImageResource(R.drawable.chat_voice_from_play);
			
			msg_media_lendth.addRule(RelativeLayout.LEFT_OF, 0);
			msg_media_lendth.addRule(RelativeLayout.RIGHT_OF, R.id.chat_conversation_msg_media);
			
//			if (mContact.isAvatar()) {
//				if (mAvatorManager.containsKeyAsAvatorBytes(mContact.getJID())) {
//					mPhoto.setImageBitmap(mAvatorManager.get(mContact.getJID()));
//				} else {
//					mAvatorManager.put(mContact.getJID(), mContact.getAvatra().toString());
//					mPhoto.setImageBitmap(mAvatorManager.get(mContact.getJID()));
//				}
//			}else {
//				mPhoto.setImageBitmap(null);
//			}

		}
		URI uri = null;
		try {
			uri = new URI(msg.getBody());

		} catch (URISyntaxException e1) {
			e1.printStackTrace();
		}
		if (  uri != null && uri.getScheme() != null) {
			mInput_media.setVisibility(View.VISIBLE);
			mInput.setVisibility(View.GONE);
			msg_media.width=LayoutParams.WRAP_CONTENT;
			mInput_media_length.setVisibility(View.GONE);
//			if (HMessage.isVideo(uri)) {
//				final MediaInfo info = HMessage.formToMediaInfo(uri);
//				int dip80=DensityUtil.dip2px(mContext, 80);
//				mInput_media.setImageResource(R.drawable.chat_icon_video_2);
//				msg_media.width=dip80;
//				mInput_media_length.setVisibility(View.VISIBLE);
//				mInput_media_length.setText(DateTimeFormatter.formatTimeAsMMSS(info.mDescript));
//			} else if (HMessage.isAudio(uri)) {
//				final MediaInfo info = HMessage.formToMediaInfo(uri);
//				
//				mInput_media_length.setVisibility(View.VISIBLE);
//				int dip100=DensityUtil.dip2px(mContext, 100);
//				msg_media.width=dip100;
//				mInput_media_length.setText(info.mDescript+"\"");
//			} else if (HMessage.isImage(uri)) {
//				final MediaInfo info = HMessage.formToMediaInfo(uri);
//				BitmapDrawable bitMapDrawable=getDrawableByPath(info.mFilePath);
//				mInput_media.setImageDrawable(bitMapDrawable);
//			} else {
//				mInput_media.setImageResource(R.drawable.arrow);
//			}
		} else {
			String faceImg = "f0[0-9]{2}|f10[0-7]"; // 正则表达式，用来判断消息内是否有表情
			try {
				SpannableString spannableString = ChatFaceExpressionUtil
						.getExpressionString(mContext, msg.getBody(), faceImg);
				mInput_media_length.setVisibility(View.GONE);
				mInput_media.setVisibility(View.GONE);
				mInput.setVisibility(View.VISIBLE);
				mInput.setText(spannableString);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			}
		}
		long time = msg.getTimestamp().getTime();
		if(c.getPosition()>0){
			
			c.moveToPosition(c.getPosition() - 1);
			TMessage toMsg = MessageTable.readLineTMessage(c);
			long mTime2 = toMsg.getTimestamp().getTime();
			boolean isOneMinute = DateTimeFormatter.isOneMinute(time, mTime2);
			if (!isOneMinute) {
				mTimeView.setText(DateTimeFormatter.format(mContext, time));
				mTimeView.setVisibility(View.VISIBLE);
			} else {
				mTimeView.setVisibility(View.GONE);
			}
			c.moveToPosition(c.getPosition()+1);
		} else {
			mTimeView.setText(DateTimeFormatter.format(mContext, time));
		}
		
		
	}

}
